using UnityEngine;
using UnityEditor;
using System.Linq;
using Prota.Unity;

using MatPropType = Prota.Unity.MaterialPropertyType;

namespace Prota.Editor
{
    [CustomEditor(typeof(MaterialPropertyProvider), false)]
    public class MaterialPropertyProviderInspector : UnityEditor.Editor
    {
        public Vector2 scroll;
        
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            
            var t = target as MaterialPropertyProvider;
            var rd = t.rd;
            if(rd == null)
            {
                EditorGUILayout.LabelField("No Renderer.");
                return;
            }
            
            EditorGUILayout.Space();
            ProtaEditorUtils.SeperateLine(2);
            var mat = rd.sharedMaterial;
            
            using(var ss = new EditorGUILayout.ScrollViewScope(scroll, GUILayout.Height(200)))
            {
                scroll = ss.scrollPosition;
                for(int i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i++)
                {
                    var name = ShaderUtil.GetPropertyName(mat.shader, i);
                    var type = ShaderUtil.GetPropertyType(mat.shader, i);
                    var applied = t.properties.Any(p => p.propertyName == name)
                        || (type == ShaderUtil.ShaderPropertyType.TexEnv && t.properties.Any(p => p.propertyName == name + "_ST"));
                    using(new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.LabelField(applied ? "√" : "-", GUILayout.Width(20));
                        EditorGUILayout.LabelField($"[{type}]{name}");
                        using(new EditorGUI.DisabledScope(applied))
                        {
                            if(GUILayout.Button("Add", GUILayout.Width(50)))
                            {
                                t.properties = t.properties.Resize(t.properties.Length + 1);
                                t.properties.Last(
                                    new MaterialPropertyEntry {
                                        propertyName = name,
                                        propertyType = type switch {
                                            ShaderUtil.ShaderPropertyType.Color => MatPropType.Color,
                                            ShaderUtil.ShaderPropertyType.Float => MatPropType.Float,
                                            ShaderUtil.ShaderPropertyType.Range => MatPropType.Float,
                                            ShaderUtil.ShaderPropertyType.TexEnv => MatPropType.Texture,
                                            ShaderUtil.ShaderPropertyType.Vector => MatPropType.Vector4,
                                            _ => MatPropType.None
                                        }
                                    }
                                );
                            }
                        }
                    }
                }
            }
        }
        
    }
}
